import requests
import time
import json
import sys
import urllib

import requests
import json
import datetime
import sys

import requests
import time
import aes
import base64
import hashlib

merchantid = "M2017071113922991567"
secretkey = "F839220E463C03DE93C1CFC2F16DC370"
aeskey = "3B65571F4EB0F92E"

# merchantid = "M2017051513600452200"
# secretkey = "1234567890123456"
# aeskey = "6543210987654321"


def pay_core(url, data_obj):
    data = json.dumps(data_obj)
    cipher = aes.AesEncryption(aeskey)
    data = cipher.encrypt(data)
    data = base64.urlsafe_b64encode(data).decode('utf-8')
    request_obj = {
        'merchantid': merchantid,
        'data': data,
        'timestamp': str(int(time.time()))
    }
    sign_string = secretkey + merchantid + data + request_obj['timestamp'] + secretkey
    sign = hashlib.sha256(sign_string.encode("utf-8")).hexdigest()
    request_obj['sign'] = sign
    body = urllib.parse.urlencode(request_obj)
    response = requests.post(url, data=request_obj)
    print(response.content)
    rsp_data = json.loads(response.content.decode('utf-8'))
    print(response.content)
    if rsp_data['message'] != 'success':
        print(response.content)
        return
    rsp_sign_string = secretkey + rsp_data['merchantid'] + rsp_data['data'] + rsp_data['timestamp'] + secretkey
    rsp_sign = hashlib.sha256(rsp_sign_string.encode('utf-8')).hexdigest()
    if rsp_sign != rsp_data['sign']:
        print("sign error")
    data = base64.urlsafe_b64decode(rsp_data['data'].encode('utf-8'))
    result = cipher.decrypt(data)
    print(result)


if sys.argv[1] == 'reg':
    data_obj = {
        "ordernumber": "reg" + str(int(time.time())),
        "username": "潘豪",
        "usertel": "13418071372",
        "usercardno": "6222023602098798689",
        "userpid": "362201198602190014",
        "bankname": "工商银行",
        "bankunion": "102100099996",
        "bankcode": "102",
        "bankabbr": "ICBC",
        "province": "广东省",
        "city": "广州市",
        "area": "天河区",
        "address": "广东省广州市",
        "feerate": "3.9",
        "sub_merchant_id": "11777776",
        "ratecode": "1003007",
        "settlefee": "50"
    }
    # url = 'http://127.0.0.1:10021/chanpay/reg'
    url = 'http://127.0.0.1:10021/chanpay/change_3'
    # url = 'https://chanpay.8bill.cn/pay/pg/merchant_reg'
    # url = 'http://127.0.0.1:9989/pay/libei/merchant_reg'
    # print(url)
elif sys.argv[1] == 'opensms':
    now = datetime.datetime.now()
    now.strftime("%Y%m%d")
    data_obj = {
        # "OUTERORDERNO": "test" + str(int(time.time())),
        "ordernumber": "test" + str(int(time.time())),
        'sub_merchant_id': "10203871",
        "username": "潘豪",
        "usertel": "13418071372",
        "usercardno": "6228755550089173",
        "userpid": "362201198602190014",
        "bankname": "工商银行",
        # "bankcode": "313",
        # "bankabbr": "GZCB",
        "amount": "10"
        # "ratecode": "1000010"
    }

    # url = 'http://127.0.0.1:10021/chanpay/open_card_sms'
    url = 'http://127.0.0.1:9987/pay/cjpay/opencard/1000010'
elif sys.argv[1] == 'settle':
    now = datetime.datetime.now()
    orderdate = now.strftime("%Y%m%d")
    data_obj = {
        # "OUTERORDERNO": str(int(time.time())),
        "ordernumber": "test" + str(int(time.time())),
        "username": "潘豪",
        "sub_merchant_id":"",
        "usertel": "13418071372",
        "usercardno": "6222023602098798689",
        "userpid": "362201198602190014",
        # 'orderdate': orderdate,
        'amount': '1000',
        "backurl": "https://chanpay.8bill.cn/testnotify",
        "returnurl": "https://chanpay.8bill.cn/testnotify",
        "bankname": "工商银行",
        "bankunion": "102100099996",
        "bankcode": "102",
        "bankabbr": "ICBC",
        "province": "广东省",
        "city": "广州市",
        "area": "天河区",
        "CFGPARA": {"agent_no": "10000154", "key": "27F299C02561193C469C15E448F475E0965151D2ADC3AC02"}
    }
    url = 'https://gw.3035pay.com/pay/unionpay/entrust/sgd0'

    # url = 'http://127.0.0.1:10021/chanpay/settle'
elif sys.argv[1] == 'settle_query':

    data_obj = {
        "OUTERORDERNO": str(int(time.time())),
        'payorderno': "1522047131",
        'orderdate': '20180326'
    }
    url = 'http://127.0.0.1:10025/sdgs/settle_query'
elif sys.argv[1] == 'changex':
    data_obj = {
        "ordernumber": "test" + str(int(time.time())),
        # 'OUTERORDERNO': "test" + str(int(time.time())),
        'sub_merchant_id': "214858102018032000000069",
        # "sub_merchant_id": "C20171227899540",
        "username": "潘豪",
        "usercardno": "6222023602098798689",
        "usertel": "13418071372",
        "userpid": "362201198602190014",
        "bankname": "工商银行",
        "bankunion": "102100099996",
        "bankcode": "102",
        "bankabbr": "ICBC",
        "province": "广东省",
        "feerate": '3.8',
        "city": "广州市"
    }
    url = 'http://127.0.0.1:9989/pay/pg/merchant_change'
    # url = 'http://127.0.0.1:10026/pg/change'
elif sys.argv[1] == 'opencard':
    data_obj = {
        "OUTERORDERNO": "test" + str(int(time.time())),
        "ordernumber": "test" + str(int(time.time())),
        'sub_merchant_id': "11777776",
        "username": "潘豪",
        "ratecode": "1003007",
        "usercardno": "6226890139688526",
        "usertel": "13418071372",
        "userpid": "362201198602190014",
        "bankcode": "313",
        "bankabbr": "GZCB",
        "cvn2": '370',
        "expdate": '2210',
        "returnurl": "https://chanpay.8bill.cn/noti/chanpay",
        "backurl": "http://127.0.0.1:9999/testnotify"
    }
    url = 'http://127.0.0.1:10021/chanpay/open_card_sms'
    # url = 'http://127.0.0.1:9989/pay/libei/opencard'
elif sys.argv[1] == 'openquery':
    data_obj = {
        "OUTERORDERNO": "test" + str(int(time.time())),
        "outerorderno": "test1513848003",
        'sub_merchant_id': "10203871",
        "ratecode": "1000008"
    }
    url = 'http://127.0.0.1:10021/chanpay/open_card_query'
elif sys.argv[1] == 'consumesms':
    data_obj = {
        "OUTERORDERNO": "test" + str(int(time.time())),
        # "ordernumber": "consume" + str(int(time.time())),
        'sub_merchant_id': "11777776",
        "username": "潘豪",
        "usertel": "13418071372",
        "userpid": "362201198602190014",
        "amount": "100000",
        "usercardno": "6226890139688526",
        "bankcode": "302",
        "bankabbr": "CITIC",
        # "usercardno": "6228755550089173",
        # "bankcode": "313",
        # "bankabbr": "GZCB",
        "backurl": "http://127.0.0.1:9999/testnotify",
        "feerate": "26",
        "settlefee": "20",
        "ratecode": "1003007"
    }
    url = 'http://127.0.0.1:10021/chanpay/consume_sms'
    # url = 'https://api.3035pay.com/pay/cjpay/merchant_pay/1000010'
    # url = 'http://127.0.0.1:9989/pay/pg/merchant_pay'
elif sys.argv[1] == 'consume':
    data_obj = {
        "OUTERORDERNO": "test" + str(int(time.time())),
        "outerorderno": "test" + str(int(time.time())),
        "payorderno": "",
        # "ordernumber": "test" + str(int(time.time())),
        "ratecode": "1003007",
        "smscode": "000000",
        "username": "潘豪",
        "userpid": "362201198602190014",
        'sub_merchant_id': "11777776",
        "amount": "20000",
        "body": "测试",
        "usercardno": "6226890139688526",
        "bankcode": "302",
        "bankabbr": "CITIC",
        "cvn2": '370',
        "expdate": '2210',
        "feerate": "3.9",
        "settlefee": "50",
        "usertel": "13418071372",
        "returnurl": " https://m1.8bill.cn/app/QR2001.php?act=gotoPay&ordernumber=QR20011803151708087x4ii7&amount=10",
        "backurl": "http://127.0.0.1:9999/testnotify",
    }
    # url = 'http://127.0.0.1:10028/libei/rate_change'
    # url = 'http://127.0.0.1:10028/libei/quickpay'
    # url = 'http://127.0.0.1:9989/pay/libei/merchant_pay'
    # url = 'http://127.0.0.1:9989/pay/pg/merchant_pay'
    # url = 'http://127.0.0.1:9987/pay/cjpay/merchant_pay_sms/1001001'
    url = 'http://127.0.0.1:10021/chanpay/consume'
elif sys.argv[1] == 'payquery':
    data_obj = {
        "OUTERORDERNO": "test" + str(int(time.time())),
        'sub_merchant_id': "210058102018030700000002",
        "outerorderno": "test1513069215",
        "payorderno": "test1520413728",
        "orderdate": "20180307"
    }
    url = 'http://127.0.0.1:10026/pg/query'
elif sys.argv[1] == "change":
    with open("CJ_USER_REG.txt", "r") as f:
        for line in f:
            line = line.strip()
            # print(line.encode())
            # break
            data_obj = {
                "feerate": "3.8",
                "settlefee": "200",
                # "maxfee": "99999900",
                # "settlerate": "0",
                # "backurl": "http://127.0.0.1:9999/testnotify",
                # "province": "广东省",
                # "city": "广州市",
                'sub_merchant_id': line,
                "ratecode": "1000010"
            }
            url = 'http://127.0.0.1:10021/chanpay/change_3'
            rsp = requests.post(url, data=json.dumps(data_obj))
            print(rsp.content)
elif sys.argv[1] == 'page':
    data_obj = {
        "ordernumber": "test" + str(int(time.time())),

        'sub_merchant_id': "10203871",
        "username": "潘豪",
        "usertel": "13418071372",
        "userpid": "362201198602190014",
        "cvn2": '741',
        "expdate": '2007',
        "usercardno": "4581230714252220",
        "bankcode": "301",
        "bankabbr": "BCOM",
        "returnurl": "https://chanpay.8bill.cn/noti/chanpay",
        "backurl": "http://127.0.0.1:9999/testnotify"
    }
    # url = 'http://123.207.247.101:10021/chanpay/page_open_card?' + urllib.parse.urlencode(data_obj)
    url = 'https://chanpay.b8ill.cn/pay/cjpay/opencard/1001001'

# print(url)
# rsp = requests.post(url, data= json.dumps(data_obj))
# print(rsp.content)
pay_core(url, data_obj)
